#include <REGX52.H>

#define uint unsigned int 
#define uchar unsigned char 

sbit WE=P2^7;
sbit DU=P2^6;

uint code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x76,0x38,0x37,0x3e,0x40,0x00};
uint keyvalue;
	
void Delay(uint z)
{
	uint x,y;
	for(x=z;x>0;x--)
		for(y=114;y>0;y--);
}

void main()
{
	P0=0XFF;
	WE=1;
	P0=0X00;
	WE=0;
	while(1)
	{
		P3=0XF0;
		if(P3!=0XF0)
		{
			Delay(20);
			if(P3!=0XF0)
			{
				switch(P3)
				{
					case 0xe0:keyvalue=0;break;
					case 0xd0:keyvalue=1;break;
					case 0xb0:keyvalue=2;break;
					case 0x70:keyvalue=3;break;
				}
				P3=0X0f;
				if(P3!=0X0f)
				{
					Delay(20);
					if(P3!=0X0f)
					{
						switch(P3)
						{
							case 0x0e:keyvalue=keyvalue;break;
							case 0x0d:keyvalue=keyvalue+4;break;
							case 0x0b:keyvalue=keyvalue+8;break;
							case 0x07:keyvalue=keyvalue+12;break;
						}
					}
					while(P3!=0X0F);
				}
			}
		}
		P3=0XFf;
		if(P3!=0XFf)
		{
			Delay(20);
			if(P3!=0XFf)
			{
				switch(P3)
				{
					case 0xFe:keyvalue=16;break;
					case 0xFd:keyvalue=17;break;
					case 0xFb:keyvalue=18;break;
					case 0xF7:keyvalue=19;break;
				}
			}
			while(P3!=0XFF);
		}
		
		
		DU=1;
		P0=table[keyvalue];
		DU=0;
		
		
	}
	
}